package code.google.jcontainer.test.constraint;

import code.google.jcontainer.resolver.MethodAnnotationResolver;
import code.google.jcontainer.resolver.MethodResolveContext;
import code.google.jcontainer.resolver.ResolverException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author <a href="mailto:jfox.young@gmail.com">Young Yang</a>
 * @create 2009-12-22 17:30:50
 */
public class IntegerConstraintAnnotationResolver implements MethodAnnotationResolver{

    private static final Log log = LogFactory.getLog(IntegerConstraintAnnotationResolver.class);

    public void resolveMethodAnnotationAfterConstruct(MethodResolveContext methodContext) throws ResolverException {

    }

    public void resolveMethodAnnotationBeforeMethod(MethodResolveContext methodContext) throws ResolverException {
        Object[] parameters = methodContext.getParameters();
        int age = (Integer)parameters[0];
        IntegerConstraint integerConstraint = (IntegerConstraint)methodContext.getAnnotation();
        int min = integerConstraint.min();
        int max = integerConstraint.max();
        if(age < min || age > max){
            throw new ParameterConstraintException("Age value " + age + " should <" + min + " &&>" + max);
        }
    }

    public void resolveMethodAnnotationAfterMethod(MethodResolveContext methodContext) throws ResolverException {

    }

}